package com.qclix.inv.textui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Provides facilities to handle flow and read user input to extending classes
 */
public abstract class AbstractActionHandler implements ActionHandler {
    private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private FlowHandler flowHandler;

    protected void setNextHandler(FlowHandler.HANDLER handler) {
        flowHandler.setNexFlow(handler);
    }

    public void setFlowHandler(FlowHandler handler) {
        this.flowHandler = handler;
    }
    
    protected String readChoice(String defaultChoice) {
        try {
            return reader.readLine().trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return defaultChoice;
    }

    protected String readValue() {
        try {
            return reader.readLine().trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    protected String readValue(String defaultValue) {
        String value = readValue();
        return value.equals("")? defaultValue : value;
    }
}
